local frame = CreateFrame("Button", "oApplication", UIParent, "SecureActionButtonTemplate AutoCastShineTemplate")
local macro = "/stopcasting\n/use %s\n/use [button:2] 17; 16"
local tip = GameTooltip
local orig = tip:GetScript"OnTooltipSetItem"

-- We could add our own spark, but this works.
for _, v in ipairs(frame.sparkles) do
	v:SetWidth(v:GetWidth() * 3)
	v:SetHeight(v:GetHeight() * 3)
end

-- From PeriodicTable
local GetID = function(item)
	if type(item) == "number" then return item
	elseif type(item) == "string" then
		local _, _, id = string.find(item, "item:(%d+):%d+:%d+:%d+")
		if id then return tonumber(id) end
	end
end
local S = function() return not TradeFrame:IsShown() and not BankFrame:IsShown() and not MerchantFrame:IsShown() and not MailFrame:IsShown() and not InCombatLockdown() and IsAltKeyDown() end
local E = '3824 34538 40773 23122 41170 41174 20844 3829 41194 43230 43234 23529 36900 12404 20745 20747 20749 8984 8985 6811 41171 22053 22055 41191 41195 43231 43235 34861 3775 28421 7964 6949 6950 21835 10920 10921 10922 3239 23123 41172 3241 2892 41192 41196 43232 10918 7965 23528 22522 22521 31535 2863 20744 20746 20748 20750 2893 2862 34539 6530 5237 36899 3240 7307 12643 8926 8927 41169 41173 6529 22054 41193 21927 6532 43237 6533 8928 28420 18262 43233 2871 6947 '

local OnShow = function(self, ...)
	if(self and orig) then orig(self, ...) end
	local name, id = self:GetItem()

	if(name and id) then
		id = GetID(id)
		if(id and E:match(id.."%s") and S()) then
			frame:ClearAllPoints()
			frame:SetAttribute("macrotext", macro:format(name))
			frame:SetAllPoints(GetMouseFocus())
			AutoCastShine_AutoCastStart(frame, 0, 1, 0)
			frame:Show()
		end
	end
end

frame:SetFrameStrata"DIALOG"
frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
frame:SetScript("OnLeave", function(self) self:Hide() end)
frame:SetAttribute("*type*", "macro")

frame:SetScript("OnEvent", function(self, event, key, state)
	if(event == "MODIFIER_STATE_CHANGED") then
		if(key == "LALT" or key == "RALT") then
			if(not InCombatLockdown() and self:IsShown()) then
				self:ClearAllPoints()
				self:Hide()
				AutoCastShine_AutoCastStop(frame)
			end
		end
	else
		self:ClearAllPoints()
		self:Hide()
		AutoCastShine_AutoCastStop(frame)
	end
end)
frame:RegisterEvent"MODIFIER_STATE_CHANGED"
frame:RegisterEvent"PLAYER_REGEN_DISABLED"

tip:SetScript("OnTooltipSetItem", OnShow)
